package com.zrrd._12_javaSE_Plus.reflect.no_19;

import java.lang.reflect.Field;
import java.util.Arrays;

/**
 * @author WanJl
 * @version 1.0
 * @title No_19
 * @description
 * @create 2023/11/18 16:50
 */
public class No_19 {
    /**
     * @param obj       对象
     * @param fieldName 属性名
     * @return 属性值
     * @throws Exception
     */
    public static Object getFieldValue(Object obj, String fieldName) throws Exception {
        //1.获取Class对象
        Class<?> clazz = obj.getClass();
        //2.获取属性对象
        Field f = clazz.getDeclaredField(fieldName);
        //检查一下。无论是否是私有属性，都要获取到
        f.setAccessible(true);
        //获取属性值
        return f.get(obj);
    }

    public static void main(String[] args) throws Exception {

        Object fieldValue = getFieldValue("hello world", "value");

        char[] value =(char[])  getFieldValue("hello world", "value");


        System.out.println(Arrays.toString(value));
    }
}
